package config;

import impl.TestAspect;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;

/**
 * Created by zuoxiao
 * on 2019/6/22.
 */
@Configuration
@ComponentScan(basePackages = {"pojo", "impl"})
@ImportResource({"classpath:beans.xml"})
//@PropertySource(value = {"classpath:data-source.properties"}, ignoreResourceNotFound = true)
@EnableAspectJAutoProxy
public class AnnotionConfig {
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public TestAspect getTestAspect() {
        return new TestAspect();
    }

    @Bean
    public static Logger logger(){
        String path="../log4j.properties";//定义配置文件路径

        URL url=AnnotionConfig.class.getResource(path);//转化配置文件路径
        try {
            ConfigurationSource source = new ConfigurationSource(
                    new FileInputStream(new File(url.getPath())),url);//找到配置文件
            Configurator.initialize(null, source);//初始化配置
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Logger logger = Logger.getLogger(AnnotionConfig.class);
        return logger;
    }
}
